<?php
namespace Service;

use Illuminate\Database\Capsule\Manager as DB;
use Library\Log;
use Model\Dict;


/**
 * DictService
 * @author los_gsy
 */
class DictService extends BaseService {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	static public function test() {
		return __METHOD__;
	}


	/**
	 * 添加单个字典项
	 * @param array $data
	 * @return int
	 */
	static public function addDict($data) {
		$now = time();
		$data['create_at'] = $now;
		$data['update_at'] = $now;
		return Dict::add($data);
	}


	/**
	 * 批量添加字典
	 * @param array $dict_list
	 * @return mixed
	 */
	static public function batchAddDict($dict_list) {
		//定义变量
		$now = time();

		//批量添加
		DB::beginTransaction();
		foreach ($dict_list as $i => $dict) {	//循环遍历添加
			if ($dict['code'] && $dict['name'] && $dict['type']) {
				$dict['create_at'] = $now;
				$dict['update_at'] = $now;
				$id = Dict::add($dict);
				if (!$id) {
					DB::rollback();
					return $i;	//返回插入失败的dict，type和k组合键重复
				}
			}
		}
		DB::commit();
		return true;
	}


	/**
	 * 删除单个字典项
	 * @param int $id
	 * @return mixed
	 */
	static public function delDict($id) {
		return Dict::del($id);
	}


	/**
	 * 批量删除字典
	 * @param array $id_list
	 * @return int
	 */
	static public function batchDelDict($id_list) {
		return Dict::delList($id_list);
	}


	/**
	 * 保存字典项
	 * @param array $data
	 * @param int $id
	 * @return int
	 */
	static public function saveDict($data, $id) {
		//检验参数
		$data['update_at'] = time();

		//保存
		return Dict::update($id, $data);
	}


	/**
	 * 批量修改字典
	 * @param array $dict_list
	 * @return mixed
	 */
	static public function batchEditDict($dict_list) {
		//定义变量
		$now = time();

		//批量修改字典
		DB::beginTransaction();
		foreach ($dict_list as $dict) {
			$dict['create_at'] = $now;
			$dict['update_at'] = $now;
			$result = Dict::update($dict['id'], $dict);
			if (!$result) {
				DB::rollback();
				return $dict['id'];	////返回更新失败的dict，type和k组合键重复
			}
		}
		DB::commit();
		return true;
	}


	/**
	 * 获取单项字典项信息
	 * @param int $id
	 * @return array
	 */
	static public function getDict($id) {
		return Dict::get($id);
	}


	/**
	 * 根据id列表，查询字典列表
	 * @param array $id_list
	 */
	static public  function  getDictList($id_list) {
		return Dict::getList($id_list);
	}


	/**
	 * 搜索字典
	 * @param string $keyword
	 * @param int $page
	 * @param int $pagesize
	 * @return array
	 */
	static public function searchDict($keyword, $page, $pagesize) {
		assert(is_string($keyword));
		assert(is_numeric($page));
		assert(is_numeric($pagesize));
		//搜索字典
		$ret = [];
		$ret['cnt'] = DB::table('dict')
			->where('code', 'like', "%{$keyword}%")
			->orWhere('name', 'like', "%{$keyword}%")
			->orWhere('type', 'like', "%{$keyword}%")
			->count();
		$ret['list'] = DB::table('dict')
			->where('code', 'like', "%{$keyword}%")
			->orWhere('name', 'like', "%{$keyword}%")
			->orWhere('type', 'like', "%{$keyword}%")
			->orderBy('code', 'asc')
			->skip(($page - 1) * $pagesize)
			->take($pagesize)
			->get();

		return $ret;
	}


	/**
	 * 搜索字典
	 * @param array $param
	 * @param int $page
	 * @param int $pagesize
	 * @return array
	 */
	static public function advanceSearchDict($param, $page, $pagesize) {
		//搜索字典
		$ret = [];
		$ret['cnt'] = DB::table('dict')
			->where('code', 'like', "%{$param['code']}%")
			->where('name', 'like', "%{$param['name']}%")
			->where('type', 'like', "%{$param['type']}%")
			->orderBy('code', 'asc')
			->count();
		$ret['list'] = DB::table('dict')
			->where('code', 'like', "%{$param['code']}%")
			->where('name', 'like', "%{$param['name']}%")
			->where('type', 'like', "%{$param['type']}%")
			->orderBy('code', 'asc')
			->skip(($page - 1) * $pagesize)
			->take($pagesize)
			->get();

		return $ret;
	}


	/**
	 * 修改单项记录时，验证重复记录
	 * @param string $type
	 * @param string $k
	 * @param int $id
	 * @return array
	 */
	static public function findSameEditDict($code, $id) {
		$result = DB::table('dict')
			->where('code', $code)
			->where('id', '!=', $id)
			->first();
		return $result;
	}


}